package org.example.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.example.domain.ChatResponse;
import org.example.domain.WxRequest;
import org.example.service.ChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
@Slf4j
public class ChatApiController {

    @Autowired
    ChatService chatService;

    @RequestMapping("/aaa")
    String home() {
        return "Hello World!";
    }

    @RequestMapping("/chat")
    public ChatResponse chat(@RequestBody WxRequest request) {
        return chatService.chatgptApi1(request);
    }

    @RequestMapping("/wxChat")
    public void wxChat(HttpServletRequest request, HttpServletResponse response, @RequestBody(required = false) String body) throws IOException {
        response.setCharacterEncoding("UTF-8");
        boolean isGet = request.getMethod().equalsIgnoreCase("get");
        if (isGet) {
            //微信token校验
            chatService.tokenCheck(request, response);
        } else {
            //post请求为客户聊天信息
            //回复空串，由客服接口异步通知客户
            //由于客服消息需要微信认证，需要企业和300元，所以只能同步返回
            chatService.receiveFromWx(body,response);
        }
    }
}
